﻿using Common.Communal;
using ERP.Domain.System;
using MediatR;
using SqlSugar;
using System.Api.Read.Applications.Commands.Roles;

namespace System.Api.Read.Applications.CommandHandlers.Roles
{
    /// <summary>
    /// 角色查询命令处理程序
    /// </summary>
    public class RoleQueryCommandHandler : IRequestHandler<RoleQueryCommand, ApiResult<List<Role>>>
    {
        /// <summary>
        /// SqlSugar客户端
        /// </summary>
        private readonly ISqlSugarClient sqlSugar;
        /// <summary>
        /// 构造函数，注入SqlSugar客户端
        /// </summary>
        /// <param name="sqlSugar">SqlSugar客户端</param>
        public RoleQueryCommandHandler(ISqlSugarClient sqlSugar)
        {
            this.sqlSugar = sqlSugar;
        }

        /// <summary>
        /// 构造函数，注入所需的服务
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>返回结果</returns>
        public Task<ApiResult<List<Role>>> Handle(RoleQueryCommand request, CancellationToken cancellationToken)
        {
            ApiResult<List<Role>> result = new ApiResult<List<Role>>();
            var res = sqlSugar.Queryable<Role>().Where(r=>r.IsDeleted==false);
            if (!string.IsNullOrWhiteSpace(request.RoleNameNo))
            {
                res = res.Where(r => r.RoleNo.Contains(request.RoleNameNo)||r.RoleName.Contains(request.RoleNameNo));
            }
            result.Success(res.ToList(), "角色查询成功");
            return Task.FromResult(result);
        }
    }
}
